package com.jcl.elasticsearch.mapper;

import com.jcl.elasticsearch.mapper.provider.SearchResumeBasicInfoSqlProvider;
import com.jcl.elasticsearch.mapper.provider.SqlProvider;
import com.jcl.elasticsearch.resume.model.SearchResumeBasicInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author liuyy
 * @date 2018/11/17
 */
public interface SearchResumeBasicInfoMapper extends SqlMapper{

    String TABLE_NAME = "search_resume_basic_info";
    String SQL_QUERY_ALL = "select * from search_resume_basic_info ";
    String SQL_QUERY_ID = "select * from search_resume_basic_info where id ";
    String SQL_DELETE_ID = "delete from search_resume_basic_info where id = #{id}";
    String SQL_TONGJI_TODAY = "select DATE_FORMAT(create_time,'%Y-%m-%d') as '日期',source_sub as '来源',count(*) as '数量' from search_resume_basic_info ";
    String SQL_GROUP_TODAY = " group by source_sub,DATE_FORMAT(create_time,'%Y-%m-%d') order by DATE_FORMAT(create_time,'%Y-%m-%d'),source_sub";

    String SQL_ORDER_UPDATE_DATE_DESC = " order by update_date desc ";

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_ALL,
            SQL_ORDER_UPDATE_DATE_DESC,
            QUERY_SCRIPT_END
    })
    List<Map<String, Object>> findAllMap();

    @Select({
            QUERY_SCRIPT_START,
            SQL_TONGJI_TODAY,
            "<if test=\"today\">where create_time > CURDATE()</if>",
            SQL_GROUP_TODAY,
            QUERY_SCRIPT_END
    })
    List<Map<String,String>> selectCountBySources(@Param("today") Boolean today);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_ALL,
            SQL_ORDER_UPDATE_DATE_DESC,
            QUERY_SCRIPT_END
    })
    List<SearchResumeBasicInfo> findAll();

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_ID,
            SQL_IN,
            FOREACH_START_LIST_IDS,
            SQL_PROPERTY_ID_VALUE,
            FOREACH_END,
            SQL_ORDER_UPDATE_DATE_DESC,
            QUERY_SCRIPT_END
    })
    List<SearchResumeBasicInfo> findList(@Param("ids") List<Integer> ids);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_ID,
            SQL_EQUAL,
            SQL_PROPERTY_ID_VALUE,
            QUERY_SCRIPT_END
    })
    Map<String, Object> findOneMap(@Param("id") Integer id);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY_ID,
            SQL_EQUAL,
            SQL_PROPERTY_ID_VALUE,
            QUERY_SCRIPT_END
    })
    SearchResumeBasicInfo findOne(@Param("id") Integer id);

    @InsertProvider(type = SearchResumeBasicInfoSqlProvider.class, method = SqlProvider.SQL_METHOD_INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(SearchResumeBasicInfo entity);

    @UpdateProvider(type = SearchResumeBasicInfoSqlProvider.class, method = SqlProvider.SQL_METHOD_UPDATE)
    int update(SearchResumeBasicInfo entity);

    @Delete({ SQL_DELETE_ID })
    int delete(@Param("id") Integer id);
}
